﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using SGC_CarteraClientes.Models;
using SGC_CarteraClientes.Persistencia;

namespace SGC_CarteraClientes.Controllers
{
    public class ProspectoController : Controller
    {
        private Cartera_DBEntities db = new Cartera_DBEntities();

        //
        // GET: /Prospecto/
        
        public ActionResult Index()
        {
            return View(db.CA_PROSPECTO.ToList());
        }
        
        /*
        public ActionResult Index(string sortOrder)
        {
            ViewBag.NameSortParm = String.IsNullOrEmpty(sortOrder) ? "name_desc" : "";
            ViewBag.DateSortParm = sortOrder == "Date" ? "date_desc" : "Date";
            var students = from s in db.CA_PROSPECTO
                           select s;
            /*
            switch (sortOrder)
            {
                case "name_desc":
                    students = students.OrderByDescending(s => s.LastName);
                    break;
                case "Date":
                    students = students.OrderBy(s => s.EnrollmentDate);
                    break;
                case "date_desc":
                    students = students.OrderByDescending(s => s.EnrollmentDate);
                    break;
                default:
                    students = students.OrderBy(s => s.LastName);
                    break;
            }
             * 
            
           // return View(students.ToList());
      //  }
    */

        //
        // GET: /Prospecto/Details/5

        public ActionResult Details(string id = null)
        {
            CA_PROSPECTO ca_prospecto = db.CA_PROSPECTO.Find(id);
            if (ca_prospecto == null)
            {
                return HttpNotFound();
            }
            return View(ca_prospecto);
        }

        //
        // GET: /Prospecto/Create

        public ActionResult Create()
        {
            ViewBag.PROSP_EMPID = new SelectList(db.CA_EMPRESA, "EMPR_ID", "EMPR_DESC");
            return View();
        }

        //
        // POST: /Prospecto/Create

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Create(CA_PROSPECTO ca_prospecto)
        {
            if (ModelState.IsValid)
            {
                try
                {
                    String l_codigo = obtcodigo("4");//Codigo 2 Correspondiente a el Canal de Distribucion.
                    ca_prospecto.PROSP_ID = l_codigo;
                    ca_prospecto.PROSP_STS = 1;
                    ca_prospecto.PROSP_FREG = DateTime.Today;
                    db.CA_PROSPECTO.Add(ca_prospecto);
                    db.SaveChanges();
                    TempData["message"] = new ClsMensaje().getHtmlMsg("S", "El prospecto ha sido registrado correctamente");

                }
                catch (Exception ex) {
                  //  Console.WriteLine(ex.Message);
                }
               // ViewBag.PROSP_EMPID = new SelectList(db.CA_EMPRESA, "EMPR_ID", "EMPR_DESC", ca_prospecto.PROSP_EMPID);
                return RedirectToAction("Index");
            }
            ViewBag.PROSP_EMPID = new SelectList(db.CA_EMPRESA, "EMPR_ID", "EMPR_DESC", ca_prospecto.PROSP_EMPID);
            return View(ca_prospecto);
        }

        //
        // GET: /Prospecto/Edit/5

        public ActionResult Edit(string id = null)
        {

            CA_PROSPECTO ca_prospecto = db.CA_PROSPECTO.Find(id.Replace("%20", "").Replace(" ", ""));
            //Console.WriteLine("'" + id + "'");            
            //ca_prospecto.PROSP_ID = id.Replace("%20", "");
            if (ca_prospecto == null)
            {
                return HttpNotFound("<pre>'" + id + "'</pre>");
            }
            ViewBag.PROSP_EMPID = new SelectList(db.CA_EMPRESA, "EMPR_ID", "EMPR_DESC", ca_prospecto.PROSP_EMPID);
            return View(ca_prospecto);
        }

        //
        // POST: /Prospecto/Edit/5

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Edit(CA_PROSPECTO ca_prospecto)
        {
            if (ModelState.IsValid)
            {
                db.Entry(ca_prospecto).State = EntityState.Modified;
                db.SaveChanges();
                TempData["message"] = new ClsMensaje().getHtmlMsg("I", "Los datos del prospecto fueron actualizados.");

                return RedirectToAction("Index");
            }
            return View(ca_prospecto);
        }

        //
        // GET: /Prospecto/Delete/5

        public ActionResult Delete(string id = null)
        {
            CA_PROSPECTO ca_prospecto = db.CA_PROSPECTO.Find(id);
            if (ca_prospecto == null)
            {
                return HttpNotFound();
            }
            return View(ca_prospecto);
        }

        //
        // POST: /Prospecto/Delete/5

        [HttpPost, ActionName("Delete")]
        [ValidateAntiForgeryToken]
        public ActionResult DeleteConfirmed(string id)
        {
            CA_PROSPECTO ca_prospecto = db.CA_PROSPECTO.Find(id);
            db.CA_PROSPECTO.Remove(ca_prospecto);
            db.SaveChanges();
            TempData["message"] = new ClsMensaje().getHtmlMsg("W", "Se eliminó el prospecto satisfactoriamente");

            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }

        public string obtcodigo(string tipo)
        {
            string cod_retorno;
            CampaniaDAO CampDAO = new CampaniaDAO();
            cod_retorno = CampDAO.GeneraCodigo(tipo);
            return cod_retorno;
        }
    }
}